import * as cacheTypes from "./cache-types";
function cacheReducer(cacheStates = {}, { type, payload }) {
  switch (type) {
    case cacheTypes.CREATE:
      return {
        ...cacheStates,
        [payload.cacheId]: {
          scrolls: new Map(), // 缓存滚动条，key: dom, value: scrollTop
          cacheId: payload.cacheId, // 缓存Id
          element: payload.element, // 需要渲染的虚拟DOM
          doms: undefined, // 当前的虚拟dom所对应的真实dom
          status: cacheTypes.CREATE, // 缓存状态
        },
      };
    case cacheTypes.CREATED:
      return {
        ...cacheStates,
        [payload.cacheId]: {
          ...cacheStates[payload.cacheId],
          doms: payload.doms,
          status: cacheTypes.CREATED,
        },
      };
    case cacheTypes.ACTIVE:
      return {
        ...cacheStates,
        [payload.cacheId]: {
          ...cacheStates[payload.cacheId],
          status: cacheTypes.ACTIVE,
        },
      };
    case cacheTypes.DESTROY:
      for (const key in cacheStates) {
        if (payload.cacheId === key) {
          return {
            ...cacheStates,
            [payload.cacheId]: {
              ...cacheStates[payload.cacheId],
              status: cacheTypes.DESTROY,
            },
          };
        } else {
          console.warn("不能给还没有创建缓存的组件设置为DESTROY");
        }
      }
    default:
      return cacheStates;
  }
}
export default cacheReducer;
